var postData = require("../../../data/post-data.js")
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
     isplay : false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var globalData = app.globalData.g_isPlay;

    var postId = options.postid;
    var datas = postData.postList[postId];
    this.data.currentPostId = postId;
    //this.data.postData = datas;

    this.setData({
      postData : datas
    });

    var posts_collected = wx.getStorageSync("posts_collected");

    if(posts_collected)
    {
      var posts_collected = posts_collected[postId];
      this.setData({
        collected: posts_collected
      });
    }else{
      var posts_collected = {};
      posts_collected[postId] = false;
      wx.setStorageSync("posts_collected", posts_collected);
    }

    if (globalData && app.globalData.g_currentMusicPostId === postId)
    {
      this.setData({
        isplay: true
      });
    }

    this.setAudioMonitor();
  },

  setAudioMonitor:function(){
    var that = this;
    wx.onBackgroundAudioPlay(function () {
      that.setData({
        isplay: true
      });
      app.globalData.g_isPlay = true;
      app.globalData.g_currentMusicPostId = that.data.currentPostId;
    });

    wx.onBackgroundAudioPause(function () {
      that.setData({
        isplay: false
      });
      app.globalData.g_isPlay = false;
      app.globalData.g_currentMusicPostId = null;
    });  
    wx.onBackgroundAudioStop(function () {
      that.setData({
        isplay: false
      });
      app.globalData.g_isPlay = false;
      app.globalData.g_currentMusicPostId = null;
    }); 
  }, 
  onCollectionTap:function(event){
    var posts_collected = wx.getStorageSync("posts_collected");
    var collected = posts_collected[this.data.currentPostId];
    collected = !collected;
    posts_collected[this.data.currentPostId] = collected;
    wx.setStorageSync("posts_collected", posts_collected);
    this.setData({
      collected : collected
    });

    wx.showToast({
      title: collected ? '收藏成功' : '取消成功',
    })
  },

  onShareTap:function(event){
    var itemList = ["分享给微信好友","分享到朋友圈","分享到QQ","分享到微博"];

    wx.showActionSheet({
      itemList: itemList,
      itemColor:"#405f80",
      success:function(res){
        wx.showModal({
          title: '用户' + itemList[res.tapIndex],
          content: '用户是否取消?' + res.cancel + "现在无法实现分享功能",
        })
      }
    })
  },

  onMusicTap:function(event){ 
    var isplay = this.data.isplay;
    if(isplay)
      {
        wx.pauseBackgroundAudio();
      
        this.setData({
          isplay: false
        });
      }
      else{
        wx.playBackgroundAudio({
          dataUrl: this.data.postData.music.url,
          title: this.data.postData.music.title
        }); 
        this.setData({
          isplay: true
        });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})